package com.atomic.test.example.repository;

import com.atomic.test.example.entity.Topic;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author dreamyao
 * @title
 * @date 2021/11/10 7:11 下午
 * @since 1.0.0
 */
public interface TopicRepository extends JpaRepository<Topic,Integer> {

    Topic findTopicById(Integer id);

    Topic findTopicByTopicTitle(String topicTitle);
    /**
     * 根据Id更新话题回复数
     * @param id
     * @param topicReplies
     * @return
     */
    @Modifying
    @Transactional
    @Query("update Topic set topicReplies=:topicReplies where id=:id")
    void updateTopicReplies(@Param("id") Integer id, @Param("topicReplies") Integer topicReplies);

    /**
     * 根据Id更新话题查看数
     * @param id
     * @param topicViews
     */
    @Modifying
    @Transactional
    @Query("update Topic set topicViews=:topicViews where id=:id")
    void updateTopicViews(@Param("id") Integer id, @Param("topicViews") Integer topicViews);

    void deleteTopicByTopicTitle(String topicTitle);
}
